package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.bean.PayAttentionCirclePostDateBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间 2017 年 09 月 11.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.fragement
 * 工程名 welbeing_pregnant
 */
public class MoreCircleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private List<PayAttentionCirclePostDateBean.DataBean> mlist = new ArrayList<>();
    private Context mcontent;


    LayoutInflater mInflater;
    private static final int TYPE_ITEM   = 0;
    private static final int TYPE_FOOTER = 1;

    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE     = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE     = 2;
    public static final int OPPEN_MORE     = 3;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;


    public MoreCircleAdapter( Context mceontn) {
        mcontent = mceontn;
        mInflater = LayoutInflater.from(mceontn);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_FOOTER){
            View itemView = mInflater.inflate(R.layout.load_more_footview_layout, parent, false);
            return new FooterViewHolder(itemView);
        }else if(viewType == TYPE_ITEM){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_more_circle_recyclerview,parent,false);
            ViewHolder viewHolder = new ViewHolder(view);
            view.setOnClickListener(this);
            return viewHolder;
        }
       return  null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof FooterViewHolder){
            FooterViewHolder footerViewHolder  = (FooterViewHolder) holder;
            switch (mLoadMoreStatus) {
                case PULLUP_LOAD_MORE:
                    footerViewHolder.tvLoadText.setText("上拉加载更多...");
                    break;
                case LOADING_MORE:
                    footerViewHolder.tvLoadText.setText("正加载更多...");
                    break;
                case NO_LOAD_MORE:
                    //隐藏加载更多
                    footerViewHolder.rl_01.setVisibility(View.GONE);
                    break;
                case OPPEN_MORE:
                    footerViewHolder.rl_01.setVisibility(View.VISIBLE);
                    break;
            }

        }else {
            ViewHolder viewHolder = (ViewHolder) holder;
            Glide.with(mcontent).load(RBConstants.SERVER_URL + "/Public/Uploads/" + mlist.get(position).cus_logo).transform(new GlideCircleTransform(mcontent)).into(viewHolder.iv_personal_head);
            viewHolder.tv_name.setText(mlist.get(position).cus_name);
            viewHolder.tv_date.setText(mlist.get(position).addtime);
            viewHolder.tv_titile.setText(mlist.get(position).po_name);
            viewHolder.tv_content.setText(mlist.get(position).content);
           // viewHolder.tv_circle_desc.setText(mlist.get(position).);
            viewHolder.tv_thumb_up.setText(mlist.get(position).state + "");
            viewHolder.tv_commentaries.setText(mlist.get(position).count);
            String po_url = mlist.get(position).po_url;
            if (po_url != null || !po_url.equals("")) {
                String[] arr = po_url.split(",");
                if (arr.length == 1) {
                    Glide.with(mcontent).load(RBConstants.SERVER_URL + "/Public/Uploads/" + arr[0]).into(viewHolder.iv_user_picture_01);
                    viewHolder.iv_user_picture_02.setVisibility(View.GONE);
                    viewHolder.iv_user_picture_03.setVisibility(View.GONE);
                } else if (arr.length == 2) {
                    Glide.with(mcontent).load(RBConstants.SERVER_URL + "/Public/Uploads/" + arr[0]).into(viewHolder.iv_user_picture_01);
                    Glide.with(mcontent).load(RBConstants.SERVER_URL + "/Public/Uploads/" + arr[1]).into(viewHolder.iv_user_picture_02);
                    viewHolder.iv_user_picture_03.setVisibility(View.INVISIBLE);
                } else {
                    Glide.with(mcontent).load(RBConstants.SERVER_URL + "/Public/Uploads/" + arr[0]).into(viewHolder.iv_user_picture_01);
                    Glide.with(mcontent).load(RBConstants.SERVER_URL + "/Public/Uploads/" + arr[1]).into(viewHolder.iv_user_picture_02);
                    Glide.with(mcontent).load(RBConstants.SERVER_URL + "/Public/Uploads/" + arr[2]).into(viewHolder.iv_user_picture_03);
                }
            } else {
                viewHolder.ll_user_picture.setVisibility(View.GONE);
            }
        }


    }
    @Override
    public int getItemViewType(int position) {

        if (position + 1 == getItemCount()) {
            //最后一个item设置为footerView
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }
    /**
     * 更新加载更多状态
     * @param status
     */
    public void changeMoreStatus(int status){
        mLoadMoreStatus=status;
        notifyDataSetChanged();
    }
    public void addItem(List<PayAttentionCirclePostDateBean.DataBean> bean) {
        mlist.addAll(bean);
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return mlist.size();
    }

    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(view);
        }
    }

    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_name;
        private  TextView tv_date;
        private  TextView tv_attention;
        private  TextView tv_titile;
        private  TextView tv_content;
        private  TextView tv_circle_desc;
        private  TextView tv_thumb_up;
        private  TextView tv_commentaries;

        private ImageView iv_personal_head;
        private ImageView iv_user_picture_01;
        private ImageView iv_user_picture_02;
        private ImageView iv_user_picture_03;
        private LinearLayout ll_user_picture;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_name =  (TextView) itemView.findViewById(R.id.item_more_circle_recyclerview_tv_name);
            tv_date =  (TextView) itemView.findViewById(R.id.item_news_post_recyclerview_tv_date);
            tv_attention =  (TextView) itemView.findViewById(R.id.item_more_circle_recyclerview_tv_attention);
            tv_titile =  (TextView) itemView.findViewById(R.id.item_more_circle_recyclerview_tv_titile);
            tv_content =  (TextView) itemView.findViewById(R.id.item_more_circle_recyclerview_tv_content);
            tv_thumb_up =  (TextView) itemView.findViewById(R.id.item_more_circle_recyclerview_tv_thumb_up);
            tv_commentaries =  (TextView) itemView.findViewById(R.id.item_more_circle_recyclerview_tv_commentaries);


            tv_circle_desc =  (TextView) itemView.findViewById(R.id.item_more_circle_recyclerview_tv_circle_desc);
            iv_personal_head =  (ImageView) itemView.findViewById(R.id.item_more_circle_recyclerview_iv_personal_head);
            iv_user_picture_01 =  (ImageView) itemView.findViewById(R.id.item_more_circle_recyclerview_iv_user_picture_01);
            iv_user_picture_02 =  (ImageView) itemView.findViewById(R.id.item_more_circle_recyclerview_iv_user_picture_02);
            iv_user_picture_03 =  (ImageView) itemView.findViewById(R.id.item_more_circle_recyclerview_iv_user_picture_03);
            ll_user_picture =  (LinearLayout) itemView.findViewById(R.id.item_more_circle_recyclerview_ll_user_picture);
        }
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {
        private ProgressBar pbLoad;
        private TextView tvLoadText;
        private RelativeLayout rl_01;
        public FooterViewHolder(View itemView) {
            super(itemView);
            pbLoad =  (ProgressBar) itemView.findViewById(R.id.pbLoad);
            tvLoadText =  (TextView) itemView.findViewById(R.id.tvLoadText);
            rl_01 =  (RelativeLayout) itemView.findViewById(R.id.load_more_footview_layout_rl_01);
        }
    }
}
